package cn.wy.pet.service.impl;

import cn.wy.basic.service.BaiduAiAuditService;
import cn.wy.basic.util.AjaxResult;
import cn.wy.basic.util.LoginContext;
import cn.wy.org.domain.Employee;
import cn.wy.org.mapper.EmployeeMapper;
import cn.wy.pet.domain.Pet;
import cn.wy.pet.domain.PetDetail;
import cn.wy.pet.domain.PetOnlineAuditLog;
import cn.wy.pet.mapper.PetDetailMapper;
import cn.wy.pet.mapper.PetMapper;
import cn.wy.pet.mapper.PetOnlineAuditLogMapper;
import cn.wy.pet.service.PetService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import cn.wy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Override
    @Transactional
    public void insert(Pet pet) {
        super.insert(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.insert(petDetail);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        PetDetail petDetail = petDetailMapper.findByPetId(id);
        petDetailMapper.delete(petDetail.getId());
        super.delete(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Employee employee = employeeMapper.findByLogininfoId(loginInfo.getId());

       for (Long id : ids) {
           Pet pet = petMapper.findOne(id);
           //智能审核
           Boolean boolean1 = baiduAiAuditService.textAudit(pet.getName());
           Boolean boolean2 = baiduAiAuditService.imageAudit(pet.getResources());
           if(boolean1 && boolean2){//审核通过
               //2.审核通过：修改状态state=1，上架时间 - 添加审核日志
               petMapper.updateOnSaleState(id,new Date());

               PetOnlineAuditLog log = new PetOnlineAuditLog();
               log.setPetId(id);
               log.setState(1);
               log.setNote("审核成功");
               log.setAuditTime(new Date());
               log.setAuditId(employee.getId());
               petOnlineAuditLogMapper.insert(log);
           }else {
               //3.审核失败：添加审核日志
               PetOnlineAuditLog log = new PetOnlineAuditLog();
               log.setPetId(id);
               log.setState(0);
               log.setNote("审核失败");
               log.setAuditId(employee.getId());
               petOnlineAuditLogMapper.insert(log);
               return AjaxResult.me().setSuccess(false).setMsg("上架失败");
           }
       }
       return new AjaxResult();
    }

    @Override
    @Transactional
    public void offsale(List<Long> ids) {
        //1.修改状态，修改下架的时间
        petMapper.updateState(ids,new Date());
    }
}
